//
//  MobileAdViewController.m
//  AdamSample2
//
//  Created by Minhye Kim on 11. 7. 13..
//  Copyright 2011 Daum Communication. All rights reserved.
//

#import "MobileAdViewController.h"
#import "MobileAdView.h"
#import "ADControl.h"

#define AD_REFRESH_PERIOD 30.0

@implementation MobileAdViewController
@synthesize superViewCtrl;

- (void)dealloc
{
    [superViewCtrl release];
    adView.addelegate = nil;
    [refreshTimer invalidate];
    refreshTimer = nil;
    [adView release];
	adView = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView
 {
 
 }
 */


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    if (adView) {
        [adView release];
        adView = nil;
	}
    
    adView = [MobileAdView requestAdWithDelegate:self];
	adView.frame = CGRectMake(0.0, 0.0, 320.0, 48.0);
	[adView retain];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [adView release];
    adView = nil;
    
}

- (void)hideAds{
    [[ADControl defaultsAD] hideAdmob:YES];
}

- (void)showAdmob{
    [[ADControl defaultsAD] hideAdmob:NO];
}


#pragma mark - MobileAdViewDelegate

-(NSString*) clientId {
    return @"AD@m 에서 발행된 키값";
}

-(UIViewController*)superViewController {
    return superViewCtrl;
}

-(void)didReceiveAd {
    CGRect convertrect = [self.view convertRect:self.view.frame fromView:self.view.superview];
    adView.frame = convertrect; 
    [self.view addSubview:adView];
    if (!refreshTimer) {
        refreshTimer = [NSTimer scheduledTimerWithTimeInterval:AD_REFRESH_PERIOD target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];
    }
    //광고수신 성공 
    NSLog(@"아담 광고 수신 성공");
    [self hideAds];
}

-(void)didFailedReceiveAdWithError:(NSError*)in_error {
	//NSLog(@"failed received : %@ timer %d", [[[in_error userInfo] allValues] lastObject], [refreshTimer isValid]);
	if (!refreshTimer) {
		refreshTimer = [NSTimer scheduledTimerWithTimeInterval:AD_REFRESH_PERIOD target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];
	}	
	//카울리 광고를 노출해준다.
    NSLog(@"아담 광고 수신 실패");
    [self showAdmob];
}

-(void)refreshAd:(NSTimer *)timer {
    [adView requestRefreshAd];
}


-(void)didReceiveRefreshAd {
    NSLog(@"아담 광고 리프레쉬 성공");
    [self hideAds];
    //리프레쉬가 성공했기 때문에 리프레쉬 실패 카운트를 다시 0으로 내려준다. 
    refreshFailCount = 0;
    
    //아담광고가 리프레쉬 될때마다 한번은 아담 한번은 카울리 이렇게 번갈아가며 보여준다. 
    /*
     rotaitAd = !rotaitAd;
     if (rotaitAd) {
     [self hideAds];
     }
     else{
     [self showCauly];
     }
     */
    
}

-(void)didFailedReceiveRefreshAdWithError:(NSError*)in_error {
    // NSLog(@"failed received refresh : %@ timer %d", [[[in_error userInfo] allValues] lastObject], [refreshTimer isValid]);
    NSLog(@"아담 광고 리프레쉬 실패");
    //[self showAdmob];
    refreshFailCount += 1;
    if (refreshFailCount == 2) {
        refreshFailCount = 0;
        NSLog(@"아담 광고 리프레쉬 2번이상 실패");
        [self showAdmob];
    }
    else{
        [self hideAds];
    }
}
 

@end
